//
//  FBDTopSelectView.m
//  NewMedSci
//
//  Created by feng on 16/2/14.
//  Copyright © 2016年 Bioon. All rights reserved.
//


#define rightItemWH  40
#define adjustNum  999
#import "FBDTopSelectView.h"
#import <SDWebImage/UIButton+WebCache.h>
#import "UIView+FBDQuickCreateUI.h"
@interface FBDTopSelectView ()<UIScrollViewDelegate>
@property (nonatomic,copy) topSelectItemBlock itemClickedBlock;
@property (nonatomic,copy) addItemBlock  comeAddItemBlock;
@property (nonatomic,copy) topSelectItemBlock itemButtonClickedBlock;

@end

@implementation FBDTopSelectView
{

    UIScrollView* bgScrollView;
    UILabel* presentLabel;
    NSMutableArray*titleLabelArray;
    NSMutableArray*buttonsArray;
    UIView*bottomLineView;  // 底部显示条
    UIButton* addButton;
    
    
}


/**
 *  FBDSelect构造函数   (用UIButton)
 *
 *  @param frame  大小
 *  @param number item的个数
 *  @param comeBlock selectBlock的回调
 *
 *  @return 返回的实例
 */
-(instancetype)initWithFrame:(CGRect)frame  withButtonUrls:(NSMutableArray*)urlsArray  selectButtonItemBlock:(topSelectItemBlock)comeBlock
{
    self=[super initWithFrame:frame];
    if (self)
    {
        self.itemButtonClickedBlock=comeBlock;
        self.buttonUrlsArray=urlsArray;
        
    }
    
    
    return self;
}
/**
 *      @author 冯宝东
 *
 *      设置默认的 索引 做相应的处理
 *
 *      @param index 索引
 *
 */
-(void)setUpButtonClickedIndex:(NSInteger)index
{
    if (_itemButtonClickedBlock&&index>=0&&index<_buttonUrlsArray.count)
    {
        self.itemButtonClickedBlock(index);
    }
    
    


}



/**
 *  FBDSelect构造函数   (用UIlabel)
 *
 *  @param frame  大小
 *  @param number item的个数
 *  @param comeBlock selectBlock的回调
 *
 *  @return 返回的实例
 */
-(instancetype)initWithFrame:(CGRect)frame  withTitles:(NSMutableArray*)titleArray  selectItemBlock:(topSelectItemBlock)comeBlock
{

    self=[super initWithFrame:frame];
    if (self)
    {
        
        self.itemClickedBlock=comeBlock;
        
        self.titleListArray=titleArray;
        CGRect addButtonCGRect=CGRectMake(ScreenW-rightItemWH-15, 3, rightItemWH, CGRectGetHeight(frame)-3*2);
        addButton=[self fbd_quickCreateUIButtonWithFrame:addButtonCGRect];
        [addButton addTarget:self action:@selector(addButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:addButton];
        
        
        UIView* sbottomLineView=[self fbd_quickCreateUIViewWithFrame:CGRectMake(0, self.frame.size.height-1, ScreenW, 1)];
        sbottomLineView.backgroundColor=[UIColor grayColor];
        [self addSubview:sbottomLineView];
        [self bringSubviewToFront:sbottomLineView];


    
    
        
    }
    
    
    return self;

}



/**
 *  FBDSelect构造函数
 *
 *  @param frame  大小
 *  @param number item的个数
 *  @param scrollAble 能否滑动
 *  @param comeBlock selectBlock的回调
 *  @param comeAddBlock comeAddBlock的回调
 *
 *  @return 返回的实例
 */
-(instancetype)initWithFrame:(CGRect)frame  withTitles:(NSMutableArray*)titleArray selectItemBlock:(topSelectItemBlock)comeBlock  withAddItemBcock:(addItemBlock)comeAddBlock
{
    self=[self initWithFrame:frame withTitles:titleArray selectItemBlock:comeBlock];
    if (self)
    {
        self.comeAddItemBlock=comeAddBlock;
    }

    return self;

}



// 配置 scrollView的 titleView
-(void)configeScrollTitleViewWithIndex:(NSInteger)index
{
    UILabel* indexLabel=[titleLabelArray objectAtIndex:index];
    [self  everyButtonPressed:indexLabel];
    presentLabel=indexLabel;
    
}

-(void)configeUIForTitleList
{
 
    

    if (bgScrollView)
    {
        [bgScrollView removeFromSuperview];
        bgScrollView=nil;
    }
    
    bgScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame)-rightItemWH, CGRectGetHeight(self.frame))];
    bgScrollView.backgroundColor=[UIColor whiteColor];
    bgScrollView.showsHorizontalScrollIndicator=NO;
    bgScrollView.delegate=self;
    [self addSubview:bgScrollView];
    
    NSInteger number=_titleListArray.count;
    if (titleLabelArray)
    {
        [titleLabelArray removeAllObjects];
        
    }else
    {
        titleLabelArray=[NSMutableArray array];
    
    }
   
    
    CGFloat totalOriginX=10;
    for (int i=0; i<number; i++)
    {
        NSString* everyStr=[_titleListArray objectAtIndex:i];
        CGRect everyCGRect=CGRectMake(totalOriginX, 0, 30, self.frame.size.height);
        UILabel* everyLabel=[self fbd_quickCreateUILabelWithFrame:everyCGRect];
        [everyLabel setView_orignX:totalOriginX];
        [self calculateLabelWidth:everyLabel comeTextStr:everyStr];
         everyLabel.text=everyStr;
        everyLabel.tag=i+adjustNum;
        everyLabel.userInteractionEnabled=YES;
        
        [titleLabelArray addObject:everyLabel];
        
        [bgScrollView addSubview:everyLabel];
        totalOriginX+=everyLabel.view_sizeWidth+20;
        
        
        UITapGestureRecognizer*everyTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPressed:)];
        [everyLabel addGestureRecognizer:everyTap];
    }
    bgScrollView.contentSize=CGSizeMake(totalOriginX, 0);
    
    if (bottomLineView)
    {
        [bottomLineView removeFromSuperview];
    }
    bottomLineView=[[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetHeight(self.frame)-3, 0, 1)];
    bottomLineView.backgroundColor=UIColorFromRGB(0x4090db);
    
    [bgScrollView addSubview:bottomLineView];
    
    
    
    presentLabel=[titleLabelArray firstObject];
    // 默认选中第一个
     [self  everyButtonPressed:presentLabel];
    
    
   
    
}
-(void)setTitleListArray:(NSMutableArray *)titleListArray
{
    if (titleListArray!=_titleListArray)
    {
        _titleListArray=titleListArray;
    }

    [self configeUIForTitleList];

}

-(void)configeUIForButtonList
{
    
    if (bgScrollView)
    {
        [bgScrollView removeFromSuperview];
        bgScrollView=nil;
    }
    
    bgScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    bgScrollView.backgroundColor=[UIColor whiteColor];
    bgScrollView.showsHorizontalScrollIndicator=NO;
    bgScrollView.delegate=self;
    [self addSubview:bgScrollView];
    
    NSInteger number=_buttonUrlsArray.count;
    if (buttonsArray)
    {
        [buttonsArray removeAllObjects];
        
    }else
    {
        buttonsArray=[NSMutableArray array];
        
    }
    
    
    CGFloat totalOriginX=10;
    for (int i=0; i<number; i++)
    {
        NSString* everyButtonUrl=[_buttonUrlsArray objectAtIndex:i];
        CGRect everyCGRect=CGRectMake(totalOriginX, 0, self.frame.size.height, self.frame.size.height);
        UIButton* everyButton=[self fbd_quickCreateUIButtonWithFrame:everyCGRect];
        [everyButton setTitle:[_buttonUrlsArray objectAtIndex:i] forState:UIControlStateNormal];
        everyButton.layer.cornerRadius=self.frame.size.height/2.0;
        [everyButton setView_orignX:totalOriginX];
        if ([everyButtonUrl hasPrefix:@"http://"])
        {
            [everyButton sd_setBackgroundImageWithURL:[NSURL URLWithString:everyButtonUrl] forState:UIControlStateNormal];
            
        }
//        [self calculateLabelWidth:everyLabel comeTextStr:everyStr];
//        everyLabel.text=everyStr;
        everyButton.tag=i+adjustNum;
        everyButton.userInteractionEnabled=YES;
        everyButton.backgroundColor=[self randomColor];
        [buttonsArray addObject:everyButton];
        
        [bgScrollView addSubview:everyButton];
        totalOriginX+=everyButton.view_sizeWidth+30;
        
        
        UITapGestureRecognizer*everyTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapButtonPressed:)];
        [everyButton addGestureRecognizer:everyTap];
    }
    
    
    bgScrollView.contentSize=CGSizeMake(totalOriginX, 0);




}


-(void)setButtonUrlsArray:(NSMutableArray *)buttonUrlsArray
{
    if (buttonUrlsArray!=_buttonUrlsArray)
    {
        _buttonUrlsArray=buttonUrlsArray;
    }
    [self configeUIForButtonList];


}

-(void)tapPressed:(UITapGestureRecognizer*)sender
{
    
    UILabel* tapSelcetView=(UILabel*)sender.view;
    if (presentLabel==tapSelcetView)
    {
        return;
    }

   [self  everyButtonPressed:tapSelcetView];


    presentLabel=tapSelcetView;
    
}

-(void)tapButtonPressed:(UITapGestureRecognizer*)sender
{

    CGFloat originX=sender.view.frame.origin.x;
    CGFloat standerOffset=originX-ScreenW/2.0+30;
    CGFloat maxOffset= bgScrollView.contentSize.width-ScreenW;
    
    CGFloat shouldOffset=standerOffset;
    if (standerOffset<0)
    {
        shouldOffset=0;
    }
    if (standerOffset>maxOffset)
    {
        shouldOffset=maxOffset;
    }
    
    NSInteger   currentIndex=sender.view.tag-adjustNum;
    

    
    [UIView animateWithDuration:0.2 animations:^{
        bgScrollView.contentOffset=CGPointMake(shouldOffset, 0);
   
        
        
    } completion:^(BOOL finished) {
     // 点击 button  label的 Block 回调
        if (self.itemButtonClickedBlock)
        {
            
            _itemButtonClickedBlock(currentIndex);
            
        }
        
    }];
    

}

-(void)everyButtonPressed:(UILabel*)sender
{
    CGFloat originX=sender.frame.origin.x;
    CGFloat standerOffset=originX-ScreenW/2.0+30;
    CGFloat maxOffset= bgScrollView.contentSize.width-ScreenW+rightItemWH;
    
    CGFloat shouldOffset=standerOffset;
    if (standerOffset<0)
    {
        shouldOffset=0;
    }
    if (standerOffset>maxOffset)
    {
        shouldOffset=maxOffset;
    }
    
    NSInteger   currentIndex=sender.tag-adjustNum;
    
    // 点击 title  label的 Block 回调
    if (self.itemClickedBlock)
    {
        
        _itemClickedBlock(currentIndex);
        
    }
    
    if (presentLabel)
    {
        presentLabel.textColor=[UIColor blackColor];
    }
    
    UILabel* currentLabel=   [titleLabelArray  objectAtIndex:currentIndex];
    currentLabel.textColor=UIColorFromRGB(0x4090db);
        [UIView animateWithDuration:0.2 animations:^{
        bgScrollView.contentOffset=CGPointMake(shouldOffset, 0);
        [bottomLineView setView_orignX:currentLabel.frame.origin.x];
        [bottomLineView setView_sizeWidth:currentLabel.frame.size.width];
        
        

    }];
    
    
    
    
    
    
    
    
}



#pragma mark --event

- (void) addButtonPressed:(UIButton*)sender
{
    [UIView animateWithDuration:0.2 animations:^{
    sender.transform=CGAffineTransformRotate(sender.transform, -M_PI_4);
        
    }];
    
    // 添加 item 的 block 回调
    if (self.comeAddItemBlock)
    {
        self.comeAddItemBlock();
    }

    




}

@end
